package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTimeUtils;
import com.ruoyi.system.domain.WhOrder;
import com.ruoyi.system.domain.WhPrinterConfig;
import com.ruoyi.system.domain.params.ManagerPrintParams;
import com.ruoyi.system.domain.params.SaveOrderParams;
import com.ruoyi.system.domain.vo.UmsCustomerInfoVo;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: 陈平安
 * @date: 2023/2/26 12:10
 * @description: 客户端api
 */
@Slf4j
@RestController
@RequestMapping("/client/api")
public class ClientController {

    @Resource
    private ISysDictDataService dictDataService;
    @Resource
    private IWhCompanyService iWhCompanyService;
    @Resource
    private IUmsCustomerService iUmsCustomerService;
    @Resource
    private IWhOrderService iWhOrderService;
    @Resource
    private IWhPrinterConfigService whPrinterConfigService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientController.class);

    /***
     * 字典国家列表
     */
    @GetMapping("/country")
    public AjaxResult countryList() {
        try {
            return AjaxResult.success("获取成功", dictDataService.countryDic());
        }catch (Exception e) {
            return AjaxResult.error("获取失败");
        }
    }

    /**
     * 客户端获取公司列表
     * @return
     */
    @GetMapping("/company")
    public AjaxResult findCompanyList() {
        try {
            return AjaxResult.success(iWhCompanyService.findCompanyList());
        }catch (Exception e) {
            LOGGER.error("获取公司列表发生错误：{}", e.getMessage());
            return AjaxResult.error("获取失败");
        }
    }

    /**
     * 客户端获取公司列表
     * @return
     */
    @GetMapping("/printer/list")
    public AjaxResult findPrinterList() {
        try {
            WhPrinterConfig whPrinterConfig = new WhPrinterConfig();
            whPrinterConfig.setStatus(2);
            List<WhPrinterConfig> list = whPrinterConfigService.selectWhPrinterConfigList(whPrinterConfig);
            List<Map<String, Object>> mapList = new ArrayList<>();
            for (WhPrinterConfig printerConfig : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", printerConfig.getId());
                map.put("label", printerConfig.getCname());
                mapList.add(map);
            }
            return AjaxResult.success(mapList);
        }catch (Exception e) {
            LOGGER.error("获取打印机列表发生错误：", e);
            return AjaxResult.error("获取失败");
        }
    }

    /**
     * 通过客户名称获取用户详情
     * @param name 客户名称
     * @return
     */
    @GetMapping("/customer/info")
    public AjaxResult clientGetCustomerInfoByName(@RequestParam String name) {
        UmsCustomerInfoVo umsCustomerInfoVo = iUmsCustomerService.clientGetCustomerInfoByName(name);
        return AjaxResult.success(umsCustomerInfoVo == null ? "" : umsCustomerInfoVo);
    }

    /**
     * 司机创建订单
     * @param params
     * @return
     */
    @PostMapping("/create/order")
    public AjaxResult createOrder(@RequestBody SaveOrderParams params) {
        log.info("添加订单入参：{}",params.toString());
        params.setCreateBy("0");
        params.setCreateTime(new Date());
        WhOrder orderInfo = iWhOrderService.insertWhOrder(params);
        return AjaxResult.success(orderInfo);
    }

    /**
     * 更新订单
     * @param params
     * @return
     */
    @PostMapping("/update/order")
    public AjaxResult updateWhOrder(@RequestBody SaveOrderParams params) {
        try {
            params.setUpdateBy("0");
            params.setUpdateTime(new Date());
            int i = iWhOrderService.updateWhOrder(params);
            return i > 0 ? AjaxResult.success("操作成功") : AjaxResult.error("操作失败");
        }catch (Exception e) {
            log.error("司机端更新订单失败：{}，操作时间：{}",e.getMessage(), DateTimeUtils.formatDateTime(new Date()));
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 更新订单
     * @param params
     * @return
     */
    @PostMapping("/print/order")
    public AjaxResult managerPrint(@RequestBody ManagerPrintParams params) {
        iWhOrderService.managerPrint(params);
        return AjaxResult.success("操作成功");
    }
}
